var SceneSpace = function(options) {
	this.ctx = options.ctx;
	this.editor = options.editor;
	this.globe = options.editor.globe;
	this.isOpen=false;
	this.isOpenEdit=true;
	this.editPModel;
	this.dIndex;
}

SceneSpace.prototype.openLibrary = function() {
	var _this = this;
	if (_this.isOpen) {
		return;
	}
	layer.load(2);
	var res = common_ajax.ajaxFunc('get', _this.ctx + "/scene/satellite/library", {}, "html", null);
	layer.closeAll('loading');
	if (!res) {
		layer.alert('系统异常！');
		return;
	}
	layer.closeAll();
	layer.open({
		id: 'satelliteLibrary',
		title: '卫星场景库',
		type: 1,
		area: '230px',
		content: res,
		shade: 0,
		offset: 'r',
		fixed: true,
		move: false,
		skin: 'layui-layer layui-layer-adminRight layui-layer-city',
		success: function(layero, dIndex) {
			_this.isOpen = true;
			$(layero).children('.layui-layer-content').css('overflow', 'hidden');
			$(layero).children('.layui-layer-content').css('height', 'calc(100% - 40px)');
			form.render();
			
			$(".ImageList>.content>.item>.Thumbnail").on('click', function() {
				//_this.editor.sceneModel.add($(this).attr('id'));
			})
			
		},
		end: function() {
			_this.isOpen = false;
		}
	});
}


SceneSpace.prototype.open = function(obj){
	var _this = this;
	if(_this.isOpen){
		_this.createEditPModel(obj);
		_this.loadForm(obj);
		return;
	}
	layer.load(2);
	var res = common_ajax.ajaxFunc('get', _this.ctx + "/scene/model/edit", {},"html", null);
	layer.closeAll('loading');
	if (!res) {
		layer.alert('系统异常！');
		return;
	}
	
	layer.open({
		id:'editScene',
	    type: 1,
	    anim:5,
	    title:'属性编辑',
	    area: '250px',
	    content:res,
	    shade:0,
	    offset: ['45px', '0px'],	
	    fixed:true,
	    move:false,
		skin:'layui-layer  layui-layer-adminRight layui-layer-city',
	    success: function (layero, dIndex) {
            $(layero).children('.layui-layer-content').css('overflow', 'hidden');
            $(layero).children('.layui-layer-content').css('height', 'calc(100% - 40px)');
            form.render();
            _this.dIndex=dIndex;
            _this.isOpen=true;
            _this.createEditPModel(obj);
            _this.loadForm(obj);
		},
		end:function(){
			if(_this.editPModel){
				_this.editPModel.destroy();
				_this.editPModel=null;
			}
			_this.isOpen=false;
		}
	});	
}

SceneSpace.prototype.loadForm = function(obj){
	var _this = this;
	let data={};
	if(obj){
		data={
			id:obj.id,
			name:obj.name||'',
			code:obj.code||'',
			sort:obj.sort||'',
			
			x:obj.position.x||'',
			y:obj.position.y||'',
			z:obj.position.z||'',
			
			heading:obj.orientation.heading||0,
			pitch:obj.orientation.pitch||0,
			roll:obj.orientation.roll||0,
			
			scale:obj.model.scale||1,
			minimumPixelSize:obj.model.minimumPixelSize||0,
			fill:obj.model.fill||0,
			fillColor:obj.model.fillColor||'',
			silhouette:obj.model.silhouette||0,
			silhouetteColor:obj.model.silhouetteColor||'',
			SceneSpaceSilhouetteSize:obj.model.SceneSpaceSilhouetteSize||0,
			distanceDisplayCondition:obj.model.distanceDisplayCondition||0,
			distanceNear:obj.model.distanceNear||0,
			distanceFar:obj.model.distanceFar||5000,
			animation:obj.model.animation||0,
			shadows:obj.model.shadows||false,
		}	
	}
	form.val('modelEditForm', data);
}

SceneSpace.prototype.close = function(){
	var _this = this;
	if(_this.dIndex){
		layer.close(_this.dIndex);	
	}
}

/**
 * 添加模型
 */
SceneSpace.prototype.add = function(modelId){
	let _this=this;
	_this.close();
	var res = common_ajax.ajaxFunc('get', _this.ctx + "/model/getById", {id:modelId},"json", null);
	if(200==res.code){
		let option={
			attribute:{
				modelId:res.data.id,
				name:res.data.name,
			},
			model:{
				url: _this.ctx +res.data.url,
			}
		};
		let drawModel=_this.globe.drawModel(option);
		drawModel.on('success',function(e){
			_this.globe.put(e.id,e);
			_this.open(e);
		});
	}
}

SceneSpace.prototype.createEditPModel = function(model){
	let _this=this;
	if(_this.isOpenEdit){
		if(_this.editPModel){
			_this.editPModel.destroy();
			_this.editPModel=null;
		}
		_this.editPModel=_this.globe.createEditPModel(model);
		_this.editPModel.on('change',function(e){
			if(e.heading && $("#SceneSpaceHeading")) {
				$("#SceneSpaceHeading").val(e.heading);
			}
		})
	}
}

SceneSpace.prototype.closeEditPModel = function(){
	let _this=this;
	if(_this.editPModel){
		_this.editPModel.destroy();
		_this.editPModel=null;
	}
}
